package com.homedo.microservice.open.service.gateway.manager.web.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.enums.ResponseEnum;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.exception.HomedoException;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.enums.forroutedomain.StatusEnum4RouteDomain;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.RouteDomain;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteDomainEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.RouteDomainQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.persistence.dao.RouteDomainDao;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.support.ApplicationNameHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月18日 11时30分20秒
 */
@Service
public class RouteDomainService extends BaseService<RouteDomain> {
    @Autowired
    private RouteDomainDao routeDomainDao;
    @Autowired
    private RouteService routeService;


    @Override
    public List<RouteDomainEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<RouteDomain> routeDomains = routeDomainDao.getListByIds(ids, RouteDomain.class);

        if (!Detect.notEmpty(routeDomains)) {
            return null;
        }

        return this.convent2Enhanceds(routeDomains);
    }


    @Override
    public List<RouteDomainEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<RouteDomain> routeDomains = routeDomainDao.getListByIdsGreaterThanMark0(ids, RouteDomain.class);

        if (!Detect.notEmpty(routeDomains)) {
            return null;
        }

        return this.convent2Enhanceds(routeDomains);
    }


    @Override
    public List<RouteDomainEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<RouteDomain> routeDomains = (List<RouteDomain>) pos;
        List<RouteDomainEnhanced> routeDomainEnhanceds = Lists.newArrayList();
        routeDomains.forEach(e -> routeDomainEnhanceds.add(new RouteDomainEnhanced(e)));
        return routeDomainEnhanceds;
    }


    public Pagination<RouteDomainEnhanced> getPagination(RouteDomainQuery routeDomainQuery, Pagination<RouteDomainEnhanced> pagination, UserContext userContext) {
        Pagination<RouteDomain> routeDomainPagination = routeDomainDao.getPagination(routeDomainQuery, pagination.copy());

        pagination.setCount(routeDomainPagination.getCount());

        if (!Detect.notEmpty(routeDomainPagination.getItems())) {
            return pagination;
        }

        pagination.setItems(this.convent2Enhanceds(routeDomainPagination.getItems()));

        return pagination;
    }

    public RouteDomain addRouteDomain(RouteDomain routeDomain, UserContext userContext) {
        this.checkAdd(routeDomain);
        routeDomain.setCreateTime(new Date());
        routeDomain.setUpdateTime(new Date());

        return routeDomainDao.saveSelective(routeDomain);
    }

    private void checkAdd(RouteDomain routeDomain) {
        if (!Detect.notEmpty(routeDomain.getApplicationName()) || !Detect.notEmpty(routeDomain.getDomain()) ||
                !Detect.notEmpty(routeDomain.getComment()) || routeDomain.getStatus() == null) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }
    }

    public void updateRouteDomain(int id, String domain, String comment, Short status, UserContext userContext) {
        RouteDomain routeDomain = routeDomainDao.getById(id);
        if (Detect.notEmpty(domain)) {
            routeDomain.setDomain(domain);
        }
        if (Detect.notEmpty(comment)) {
            routeDomain.setComment(comment);
        }
        if (status != null) {
            routeDomain.setStatus((int) StatusEnum4RouteDomain.getStatusEnumByCode(status).getCode());
        }
        routeDomain.setUpdateTime(new Date());

        routeDomainDao.updateByPrimaryKeySelective(routeDomain);
    }

    public RouteDomainEnhanced appendRouteEnhanceds(RouteDomainEnhanced routeDomainEnhanced, UserContext userContext) {
        this.appendRouteEnhanceds(Lists.newArrayList(routeDomainEnhanced), userContext);
        return routeDomainEnhanced;
    }

    public List<RouteDomainEnhanced> appendRouteEnhanceds(List<RouteDomainEnhanced> routeDomainEnhanceds, UserContext userContext) {
        //取出此应用中的所有的路由
        List<RouteEnhanced> routeEnhanceds = routeService.getEnhanceds4Application(ApplicationNameHolder.get(), null, userContext);

        if (!Detect.notEmpty(routeEnhanceds)) {
            return routeDomainEnhanceds;
        }
        this.appendRouteEnhanceds(routeDomainEnhanceds, routeEnhanceds, userContext);

        return routeDomainEnhanceds;
    }

    private void appendRouteEnhanceds(List<RouteDomainEnhanced> routeDomainEnhanceds, List<RouteEnhanced> routeEnhanceds, UserContext userContext) {
        routeDomainEnhanceds.forEach(e -> this.appendRouteEnhanceds(e, routeEnhanceds, userContext));
    }

    private void appendRouteEnhanceds(RouteDomainEnhanced routeDomainEnhanced, List<RouteEnhanced> routeEnhanceds, UserContext userContext) {
        List<RouteEnhanced> curRouteEnhanced = Lists.newArrayList();

        routeEnhanceds.forEach(e -> {
            if (e.getRouteDomainEnhanced() != null && e.getRouteDomainEnhanced().getId().intValue() == routeDomainEnhanced.getId()) {
                curRouteEnhanced.add(e);
            }
        });

        routeDomainEnhanced.setRouteEnhanceds(curRouteEnhanced);
    }

    public void deleteRouteDomain(int id, UserContext userContext) {
        RouteDomainEnhanced routeDomainEnhanced = this.getEnhanced(id, userContext);
        this.appendRouteEnhanceds(routeDomainEnhanced, userContext);
        if (Detect.notEmpty(routeDomainEnhanced.getRouteEnhanceds())) {
            throw new HomedoException("[已绑定路由不能删除，请先解绑！]");
        }
        routeDomainDao.deleteById(id);
    }
}
